/*
 * @Author: xushuhao
 * @Email: xshmouse@163.com
 * @Date: 2020-04-19 21:49:43
 * @Last Modified by: xushuhao
 * @Last Modified time: 2020-04-19 21:50:26
 * @Description: 基类基于页面层级的状态管理
 * 这个管理类，只是标记 当前 按照顺序放入和移除栈名称，并不是页面跳转后退 的功能， 只是方便 推算、表示生命周期方法
 */


import 'base_widget.dart';

class NavigatorManger {
  List<String> _activityStack = new List<String>();

  NavigatorManger._internal();

  static NavigatorManger _singleton = new NavigatorManger._internal();

  //工厂模式
  factory NavigatorManger() => _singleton;
  void addWidget(BaseWidgetState widgetName) {
    _activityStack.add(widgetName.getWidgetName());
  }

  void removeWidget(BaseWidgetState widgetName) {
    _activityStack.remove(widgetName.getWidgetName());
  }

  bool isTopPage(BaseWidgetState widgetName) {
    if (_activityStack == null) {
      return false;
    }
    try {
      int _widgetNameEndIndex = widgetName.getWidgetName().indexOf('(');
      int _currentActivityStackEndIndex = _activityStack[_activityStack.length - 1].indexOf('(');
      return widgetName.getWidgetName().substring(0, _widgetNameEndIndex) ==
          _activityStack[_activityStack.length - 1].substring(0, _currentActivityStackEndIndex);
          // "DemoPage(state: _DemoPageState#b7806)" == 
          // "DemoPage(dirty, state: _DemoPageState#b7806(lifecycle state: created))"
    } catch (exception) {
      return false;
    }
  }

  bool isSecondTop(BaseWidgetState widgetName) {
    if (_activityStack == null) {
      return false;
    }
    try {
      int _widgetNameEndIndex = widgetName.getWidgetName().indexOf('(');
      int _currentActivityStackEndIndex = _activityStack[_activityStack.length - 2].indexOf('(');
      return widgetName.getWidgetName().substring(0, _widgetNameEndIndex) ==
          _activityStack[_activityStack.length - 2].substring(0, _currentActivityStackEndIndex);
    } catch (exception) {
      return false;
    }
  }
}
